package com.example.demo.Controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.example.demo.entity.Goods;
import com.example.demo.Service.IGoodsService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * goodsController
 * 
 * @author ruoyi
 * @date 2025-04-09
 */
@RestController
@RequestMapping("/system/goods")
public class GoodsController
{
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询goods列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Goods goods, @RequestParam(defaultValue = "1")int pageNum, @RequestParam(defaultValue = "3")int pageSize)
    {
        PageResult pageResult=goodsService.selectGoodsList(goods,pageNum,pageSize);
        return ResponseResult.createBySuccess(200,pageResult,"查询成功");
    }
    //不分页
    @PassToken
    @GetMapping("/frontlist")
    public ResponseResult list1(Goods goods)
    {
        System.out.println(goods.getCid());
        List<Goods> list = goodsService.selectGoodsListNoPage(goods);
        return ResponseResult.createBySuccess(200,list,"查询成功");
    }
// GoodsController.java 新增
    /**
     * 根据分类ID查询商品列表
     */
    @PassToken
    @GetMapping("/a/{cid}")
    public ResponseResult getByCategory(@PathVariable("cid") Long cid) {

        List<Goods> list = goodsService.selectGoodsByCid(cid);
        return ResponseResult.createBySuccess(200,list, "查询成功");
    }
    /**
     * 导出goods列表
     */
//    @PreAuthorize("@ss.hasPermi('system:goods:export')")
//    @Log(title = "goods", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Goods goods)
//    {
//        List<Goods> list = goodsService.selectGoodsList(goods);
//        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
//        util.exportExcel(response, list, "goods数据");
//    }

    /**
     * 获取goods详细信息
     */
@PassToken
    @GetMapping(value = "/{gid}")
    public ResponseResult getInfo(@PathVariable("gid") Long gid)
    {
      Goods goods=goodsService.selectGoodsByGid(gid);
      return ResponseResult.createBySuccess(200,goods,"查询成功");
    }

    /**
     * 新增goods
     */
    @PostMapping
    public ResponseResult add(@RequestBody Goods goods)
    {
        int i=goodsService.insertGoods(goods);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","添加成功");
        }else {
            return ResponseResult.createBySuccess(200,"","添加失败");
        }
    }

    /**
     * 修改goods
     */

    @PutMapping
    public ResponseResult edit(@RequestBody Goods goods)
    {
        int i=goodsService.updateGoods(goods);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","更新成功");
        }else {
            return ResponseResult.createBySuccess(200,"","更新失败");
        }
    }

    /**
     * 删除goods
     */

	@DeleteMapping("/{gids}")
    public ResponseResult remove(@PathVariable Long[] gids)
    { int i=goodsService.deleteGoodsByGids(gids);
        if(i>0){
            return ResponseResult.createBySuccess(200,"","删除成功");
        }else {
            return ResponseResult.createBySuccess(200,"","删除失败");
        }
    }
}
